package com.example.springboot_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_admin.dao.ISpusDao;
import com.example.springboot_admin.pojo.Product_tags;
import com.example.springboot_admin.pojo.Spus;
import com.example.springboot_admin.service.IProduct_tagsService;
import com.example.springboot_admin.service.ISpusService;
import com.example.springboot_admin.vo.ChangeTags;
import com.example.springboot_admin.vo.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author svblack
 * @since 2024-09-14
 */
@Service
public class SpusServiceImpl extends ServiceImpl<ISpusDao, Spus> implements ISpusService {
    @Autowired
    private ISpusDao spusDao;
    @Autowired
    private IProduct_tagsService productTagsService;

    @Override
    public List<Spus> search(String keyword) {
        return spusDao.search(keyword);
    }

    @Override
    public ResponseMsg changeTags(ChangeTags tags) {
        if(tags.getTags().length>0){
            UpdateWrapper<Product_tags> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("spu_id",tags.getSpuid());
            productTagsService.remove(updateWrapper);
            List<Product_tags> list = new ArrayList<>();
            for (int tid : tags.getTags()) {
                Product_tags pt = new Product_tags(tags.getSpuid(),tid);
                list.add(pt);
            }
            productTagsService.saveBatch(list);
            return ResponseMsg.success("操作成功");
        }
        return ResponseMsg.fail("操作失败");
    }

}
